﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾处置记录
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RecordController : ControllerBase
    {
        /// <summary>
        ///  获取当前处置流程编号(RecordType)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<List<LastRecordModel>> GetLast(LastRecordSearchRequest obj)
        {
            ResultDto<List<LastRecordModel>> result = new ResultDto<List<LastRecordModel>>();
            try
            {
                result.FirstParam = BLL.RecordBLL.Instance.GetLast(obj.FireIds, obj.Types);
                return result;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Record/GetLast Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        ///  处置列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> Get(RecordSearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                return BLL.RecordBLL.Instance.Get(obj.FireId, obj.Types);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Record/Get Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        ///  根据用户 角色权限 获取处置流程配置项
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetRecordConfig(RecordConfigSearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                List<Firerecordconfig> frcList= BLL.FireRecordConfigBLL.Instance.Get(obj.RecordConfigId,obj.RoleLeveId,obj.RecordType);
                if (frcList != null)
                {
                    List<dynamic> list = new List<dynamic>();
                    foreach (var item in frcList)
                    {
                        List<int> recordTypes = new List<int>();
                        item.SubRecordTypes.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                            .ToList().ForEach(f => recordTypes.Add(Convert.ToInt32(f)));
                        list.Add(new { item.RecordConfigId, item.RoleLevelId, item.RecordType, item.Sort, SubRecordTypes = recordTypes, Condition=item.Condition });
                    }
                    result.FirstParam =list;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Record/GetRecordConfig Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        ///  设置某一步骤的可选下一步
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> AddRecordConfig(RecordConfigRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                Firerecordconfig info = new Firerecordconfig();
                info.RecordConfigId = obj.RecordConfigId;
                info.RecordType = obj.RecordType;
                info.Sort = obj.Sort;
                info.RoleLevelId = obj.RoleLevelId;
                info.SubRecordTypes = string.Join(",", obj.SubRecordTypes);
                result.FirstParam = BLL.FireRecordConfigBLL.Instance.Add(info);
                return result;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Record/AddRecordConfig Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }


        /// <summary>
        ///  根据用户 Role 获取全部可用处置类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetRecordTypeByRoleLevelId(RecordTypeRoleLevelSearchRequest obj) 
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {

                var records = FireInfoTool.RecordEnum();
                var roleLevels = UserBLL.Instance.GetRoleLevel();
                var superRecord = new List<int>() { 7,9,11,12,13,14,18,19,20,24,30,31 };
                if (obj.RoleLeveId == 101201) //区县
                {
                    result.FirstParam = records.Where(p => !superRecord.Contains(p.Key)).Select(s=>s.Key).ToList();
                }
                else if(obj.RoleLeveId==101201301)//乡镇
                {
                    result.FirstParam = records.Where(p => superRecord.Contains(p.Key)).Select(s => s.Key).ToList();
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Record/GetRecordTypeByRoleLevelId Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }
    }
}